package org.blame.rabbitmqadvancedfeature.controller;

import org.blame.rabbitmqadvancedfeature.config.RabbitTemplateConfig;
import org.blame.rabbitmqadvancedfeature.config.TransTemplateConfig;
import org.blame.rabbitmqadvancedfeature.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/producer")
public class AckController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitTemplate confirmRabbitTemplate;

    @Autowired
    @Qualifier("transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;



    @RequestMapping("/ack")
    public String ack(){
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack","consumer ack ");
        System.out.println("消息发送成功");
        return "消息发送成功";
    }

    @RequestMapping("/pres")
    public String pres(){
        Message message=new Message("pres 持久化测试".getBytes(),new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE,"pres",message);
        return "消息发送成功";
    }

    @RequestMapping("confirm")
    public String confirm(){
        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"123","confirm .....",correlationData);
        return "消息发送成功";
    }

    @RequestMapping("returns")
    public String returns(){
        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"123","returns......",correlationData);
        return "消息发送成功";

    }

    @RequestMapping("retry")
    public String retry(){
        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"123","retry.....",correlationData);
        return "消息发送成功";
    }


//    设置消息的ttl
    @RequestMapping("ttl")
    public String ttl(){
        System.out.println("ttl");
        String ttlTime="30000"; //10秒
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"123","ttl...........",message -> {
            message.getMessageProperties().setExpiration(ttlTime);
            return message;
        });
        return "消息发送成功";
    }

//    设置队列的ttl
    @RequestMapping("ttl2")
    public String ttl2(){
        System.out.println("ttl2");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"123","ttl2...........");
        return "消息发送成功";
    }

    @RequestMapping("del")
    public String del(){
        System.out.println("del");
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","del...........");
        }
        return "消息发送成功";
    }


    @RequestMapping("delay1")
    public String delay1(){
//        System.out.println("del");
//        System.out.println(new Date());
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE2,"normal2","delay...........");

        System.out.println("delay");
        String ttlTime="20000"; //20秒
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE2,"normal2","666666..........."+new Date(),message -> {
            message.getMessageProperties().setExpiration(ttlTime);
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE2,"normal2","5555555....."+new Date(),
                message ->{message.getMessageProperties().setExpiration("10000");
                    return message;});
        return "消息发送成功";
    }

    @RequestMapping("delay")
    public String delay(){
        System.out.println("delay");
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","delay1111....."+new Date(),
                message ->{message.getMessageProperties().setDelayLong(20000L);
                    return message;});

        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","delay5555....."+new Date(),
                message ->{message.getMessageProperties().setDelayLong(10000L);
                    return message;});

        return "消息发送成功";
    }

//    @Transactional
    @RequestMapping("trans")
    public String trans(){
        System.out.println("delay");
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans.......");
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans.......");
        int sum=2/0;
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans.......");
        return "消息发送成功";
    }


    @RequestMapping("qos")
    public String qos(){
        System.out.println("qos");
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","qosqosqos");
        }
        return "消息发送成功";
    }
}
